/*
 ------------------------------------------------------------------------------
	sdlthread - a test program for a thread library including thread
				message management.

	Copyright (C) 2011 Pierre Veber
	
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 3 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software Foundation,
	Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
------------------------------------------------------------------------------
*/

#include "common.h"
#include "sdlthread.h"

#define THIS_FILTER	TRACE_THREAD

SDL_Surface* pScreen;

static int ThreadProc(Handle_t* pHandle, void* pData)
{
	ThreadMessage_t msg;
	int nPopped;
	
	while (1)
	{
		nPopped = GetMessage(pHandle, &msg);
		if (nPopped == -1)
		{
			TRACE("ThreadProc() - error getting message from msg queue.\n");
			return -1;
		}
		else if (nPopped == 1)
		{
			TRACE("ThreadProc() - got message code 0x%x.\n", msg.iMsgCode);
			if (msg.iMsgCode == 0xdeadbeef)
			{
				TRACE("ThreadProc() - exit message!\n");
				break;
			}
		}
		usleep(1000000);
	}
	
	return 0;
}

int main(int argc, char* argv[])
{
	SDL_Event event;
	SDL_Surface* pImageSurface;
	Handle_t* pThread;
	ThreadMessage_t msg;

	if (SDL_Init(SDL_INIT_VIDEO) < 0)
	{
		TRACE("Could not initialize SDL: %s\n", SDL_GetError());
		return 1;
	}
	
	pScreen = SDL_SetVideoMode(320, 320, 0, SDL_HWPALETTE);
	if (pScreen == NULL)
	{
		TRACE("Couldn't set screen mode: %s\n", SDL_GetError());
		return 1;
	}

	SDL_WM_SetCaption("willy-player/PROOF/6", NULL);
	SDL_WM_SetIcon(IMG_Load("res/icon.png"), NULL);

	pImageSurface = IMG_Load("res/app-background.png");
	if (pImageSurface == NULL)
	{
		TRACE("Unable to load image.\n");
		return 1;
	}
	SDL_BlitSurface(pImageSurface, NULL, pScreen, NULL);
	SDL_UpdateRect(pScreen, 0, 0, 320, 320);
	SDL_FreeSurface(pImageSurface);
	
	pThread = CreateThread(ThreadProc, NULL);
	if (pThread == NULL)
	{
		TRACE("Unable to create thread.\n");
		return 1;
	}
	
	while(SDL_WaitEvent(&event))
	{
		if (event.type == SDL_QUIT)
		{
			return 0;
		}
		else if (event.type == SDL_KEYDOWN)
		{
			switch (event.key.keysym.sym)
			{
				case SDLK_ESCAPE:
					return 0;
			
				case SDLK_RIGHT:
					msg.iMsgCode = 0x00000002;
					PostThreadMessage(pThread, &msg);
					break;

				case SDLK_SPACE:
					msg.iMsgCode = 0xdeadbeef;
					PostThreadMessage(pThread, &msg);
					break;

				default:
					break;
			}
		}
	}
	
	SDL_Quit();

	return 0;
}

#ifdef _MSC_VER

#pragma comment(lib, "../common/sdl/SDL.lib")
#pragma comment(lib, "../common/sdl/SDL_Image.lib")
#pragma comment(lib, "../common/zlib/zlib.lib")

static char* _argv[] = { "sdlthread.exe", "" };
int APIENTRY _tWinMain(
			HINSTANCE hInstance,
			HINSTANCE hPrevInstance,
			LPTSTR    lpCmdLine,
			int       nCmdShow
			)
{
	/* convert command line to UTF-8 */
	char* pszCmdLine = (char*)malloc(_tcslen(lpCmdLine)+1);
	WideCharToMultiByte(CP_UTF8, 0, lpCmdLine, -1 ,pszCmdLine, _tcslen(lpCmdLine)+1, NULL, NULL);
	_argv[1] = pszCmdLine;
	return main(0, _argv);
}

#endif
